<?php
class Menu extends ModelBase
{
	private $tableName = 'myadmin_menu_url';
	
	public static function getInstance()
	{
		return parent::instance(__CLASS__);
	}
	
	public function getMenuInfo($mid)
	{
		$menu = Yii::app()->db->createCommand()
		->select('*')
		->from($this->tableName)
		->where('menu_id=:menu_id', array(':menu_id'=>$mid))
		->queryRow();
		return $menu;
	}
	
	public function getMenuByModuleId($module_id,$type ='bar')
	{
		$typestr = '1 ';
		switch ($type)
		{
			case 'bar':
				$typestr = 'is_show  = 1 and online =1 ';
				break;
			case 'permission':
				$typestr = ' online =1 ';
				break;
		}
		
		$menu = Yii::app()->db->createCommand()
		->select('*')
		->from($this->tableName)
		->where($typestr .'and module_id = :module_id',array(":module_id"=>$module_id))
		->queryAll();
		return $menu;
	}
	
	
	public function getMenuByUserPermission($permission,$appid = 1)
	{
		$whereStr =  $_SESSION['uid'] == 1 ? '' : 'and  a.menu_id in('.$permission.')';
		$menu = Yii::app()->db->createCommand()
		->select('a.*,b.*')
		->from($this->tableName.' a')
	    ->leftJoin('myadmin_module b', 'b.module_id = a.module_id')
	    ->leftJoin('myadmin_app c', 'c.app_id = b.app_id')
		->where('a.is_show  = 1 and a.online =1 and c.app_id =:app_id '.$whereStr,array(':app_id'=>$appid))
		->queryAll();
		return $menu;
	}
	
	
	public function getAllMenu($mid,$menu_name,$limit,$offset)
	{
		$where = ' 1 ';
		if ($mid) $where .=' and a.module_id = '.$mid;
		if ($menu_name) $where .=' and a.menu_name = "'.$menu_name.'"';
		$menu = Yii::app()->db->createCommand()
		->select('a.* ,b.module_name,c.menu_name as fater_menu_name')
		->from($this->tableName.' a')
		->leftJoin('myadmin_module b', 'b.module_id =a.module_id')
		->leftJoin($this->tableName.' c', 'c.menu_id =a.father_menu')
		->where($where)
		->limit($limit,$offset)
		->queryAll();
		return $menu;
	}
		
	public function getMenuCount($mid,$menu_name)
	{
		$where = ' 1 ';
		if ($mid) $where .=' and module_id = '.$mid;
		if ($menu_name) $where .=' and menu_name = "'.$menu_name.'"';
		$menu = Yii::app()->db->createCommand()
		->select('count(*) as num')
		->from($this->tableName)
		->where($where)
		->queryRow();
		return $menu;
	}
	
	public function getAllMenuByModuleId($module_id)
	{
		$menu = Yii::app()->db->createCommand()
		->select('*')
		->from($this->tableName)
		->where('module_id = :module_id',array(":module_id"=>$module_id))
		->queryAll();
		return $menu;
	}
	
	public function changeModuleId($ids,$module_id)
	{
		$ret = Yii::app()->db->createCommand()->update($this->tableName, array('module_id' => $module_id),
				array('in', 'menu_id', $ids));
		return $ret;
	}
	
	
	public function insertMenu($data)
	{
		$ret = Yii::app()->db->createCommand()->insert($this->tableName,$data);
		return $ret; 
	}
	
	public function updateMenu($data,$menu_id)
	{
		if(!$menu_id)
		{
			return false;
		}
		$ret = Yii::app()->db->createCommand()->update($this->tableName,$data,'menu_id = '.$menu_id);
		return $ret;
	}
	
	public function delMenu($menu_id)
	{
		if(!$menu_id)
		{
			return false;
		}
		$ret = Yii::app()->db->createCommand()->delete($this->tableName,'menu_id = '.$menu_id);
		return $ret;
	}
	
	public function getMenuByUrl($url)
	{
		$menu = Yii::app()->db->createCommand()
		->select('a.*,b.menu_name as father_name,b.menu_url as father_url')
		->from($this->tableName.' a')
		->leftJoin($this->tableName.' b', 'b.menu_id = a.father_menu')
		->where('a.menu_url=:menu_url', array(':menu_url'=>$url))
		->queryRow();
		return $menu;
	}
}